﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SiGeSIB.Data.Common;
using SiGeSIB.Data.Security;
using SiGeSIB.Data.Providers;
using SiGeSIB.DataFactory;
using SiGeSIB.Business.Impl.Providers;

namespace SiGeSIB.Business.Impl
{
	public class SecurityController : BaseController
	{
		public User Login(string login, string password)
		{
			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();
			
			User user = secProv.GetUser(login);

			if (user == null || user.Password != password)
				throw new InvalidLoginException("InvalidCredentials", "Login e/ou Senha inválidos.");

			return user;
		}

		public User GetUser(int idUser)
		{
			List<User> users = null;

			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();

			users = secProv.GetUsers(new int[]{idUser});

			return
				users.IsNullOrEmpty() ? null : users[0];
		}
		public List<User> GetUsers(int[] ids)
		{
			List<User> users = null;

			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();

			users = secProv.GetUsers(ids);

			return
				users;
		}

		public int InsertUser(User user) 
		{
			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();

			return secProv.InsertUser(user);
		}
		public void UpdateUser(User user)
		{
			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();
			secProv.UpdateUser(user);
		}
		public void RemoveUser(int id)
		{
			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();
			secProv.RemoveUser(id);
		}
		
		public EPermissions GetUserPermission(int idUser)
		{
			EPermissions permission;

			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();

			permission = secProv.GetUserPermission(idUser);

			return
				permission;
		}
		public void SaveUserPermission(int userId, EPermissions permission)
		{
			ISecurityDataProvider secProv = _DataProvider.CreateSecurityDataProvider();

			secProv.SaveUserPermission(userId, permission);
		}
	}
}
